﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using PropertyManagement.Domain.Entity;
using PropertyManagement.Business.Common;

namespace PropertyManagement.WebUI.Infrastructure.Attribte
{
    public class RequiresAuthenticationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //You can put your check here. This particular
            //check is for default asp.net membership authentication
            Customer customer = (Customer)HttpContext.Current.Session[Constant.SESSION_CUSTOMER];
            if (customer == null)
            {
                RedirectToLogin(filterContext);
            }
        }

        private void RedirectToLogin(ActionExecutingContext filterContext)
        {
            var redirectTarget = new RouteValueDictionary
            {
                {"action", "LogOn"},
                {"controller", "CustomerAccount"}
            };

            filterContext.Result = new RedirectToRouteResult(redirectTarget);
        }
    }
}